<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>calculator</title>
    <style>
        .container {
            width: 600px;
            margin: 0 auto;
        }

        label {
            display: inline-block;
            vertical-align: middle;
        }

        input, select {
            padding: 0 5px;
            text-align: center;
            width: 90px;
            border: 1px solid #5e5e5e;
            outline: none;
            height: 30px;
            line-height: 30px;
        }

        select {
            width: 50px !important;
        }

        label span {
            padding: 0 10px;
            color: #ff3333;
            border: 1px solid #5e5e5e;
            height: 30px;
            line-height: 30px;
            display: inline-block;
            margin-left: 10px;

        }

        input[type="submit"] {
            background: #0087d8;
            border-color: #0087d8;
            color: #fff;
            cursor: pointer;
        }

        input:active, input:focus {
            border-color: #da7d2b;
        }

        .result {
            min-width: 50px;
        }

        i {
            font-style: normal;
        }

        .tip {
            position: absolute;
            height: 25px;
            line-height: 25px;
            padding: 0 10px;
            background-color: #292929;
            color: white;
            font-size: 12px;
            border-radius: 5px;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .tip:before {
            content: '';
            position: absolute;
            border-style: solid;
            border-width: 8px;
            border-color: transparent transparent #292929 transparent;
            flex: 1;
            margin-top: -21px;
        }
    </style>
</head>
<body>
<div class="container">
    <form onsubmit="return false">
        <label>
            <input type="tel" id="number1" data-tip-id="number1">
        </label>
        <label>
            <select id="action">
                <option>+</option>
                <option>-</option>
                <option>*</option>
                <option>/</option>
            </select>
        </label>
        <label>
            <input type="tel" id="number2" data-tip-id="number2">
        </label>
        <label class="result"><i>=</i><span id="result">0</span></label>
        <label>
            <input type="submit" value="计算" id="compute">
        </label>
    </form>
</div>
<script src="js/jquery-1.12.0.min.js"></script>
<script src="js/tip.js"></script>
<script>
    var number1 = null;
    var number2 = null;
    var result = null;
    $(document).ready(function () {
        number1 = $('#number1');
        number2 = $('#number2');
        result = $('#result');
        number1.on('blur', checkValIsNumber);
        number2.on('blur', checkValIsNumber);
        $('#compute').on('click', compute);
    });

    function checkValIsNumber() {
        var el = $(this);
        var val = Number(el.val());
        if (isNaN(val)) {
            el.tip('只能输入数字,也不能为空');
        }
        return val;
    }

    function compute() {
        var num1 = checkValIsNumber.apply(number1);
        var num2 = checkValIsNumber.apply(number2);
        if (isNaN(num1) || isNaN(num2)) {
            return false;
        }
        var action = $('#action').val();
        var resultData = 0;
        switch (action) {
            case '+': {
                resultData = num1 + num2;
                break;
            }
            case '-': {
                resultData = num1 - num2;
                break;
            }
            case '*': {
                resultData = num1 * num2;
                break;
            }
            case '/': {
                if (num2 !== 0) {
                    resultData = num1 / num2;
                } else {
                    number2.tip('被除数不能为0');
                }
                break;
            }
            default: {
                alert('计算方式不正确');
                break;
            }
        }
        result.text(resultData);
    }
</script>
</body>
</html>